﻿using System;
using S7.Net;
using S7NetPlusNetCoreSample.S7NetPlusWrapper;

namespace S7NetPlusNetCoreSample
{
    /// <summary>
    /// S7netplus .NET Core Sample
    /// </summary>
    class Program
    {
        static void Main()
        {
            var s7PlcHelper = new S7PlcHelper();
            s7PlcHelper.Init(CpuType.S71500, "192.168.200.10", 102, 0, 1);
            s7PlcHelper.OpenAsync(default).Wait(1000);
            if (!s7PlcHelper.IsConnected && !s7PlcHelper.IsOnline)
            {
                Console.WriteLine($@"Connect PLC 192.168.200.10 failed, error message: {s7PlcHelper.LastErrorMsg}.");
                return;
            }

            DoTest(s7PlcHelper);

            s7PlcHelper.UnInit();
            Console.WriteLine(@"Press any key to exit...");
            Console.ReadKey();
        }

        private static void DoTest(S7PlcHelper s7PlcHelper)
        {
            try
            {
                byte val = 99;
                s7PlcHelper.WriteVariable("VB10", val);
                if (s7PlcHelper.ReadVariable("VB10", out var value))
                {
                    var result = (byte) value;
                    Console.WriteLine($@"Value of variable VB10 is: {result}");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            try
            {
                ushort val = 333;
                s7PlcHelper.WriteVariable("DB1.DBW20", val);
                if (s7PlcHelper.ReadVariable("DB1.DBW20", out var value1))
                {
                    var result = (ushort) value1;
                    Console.WriteLine($@"Value of variable DB1.DBW20 is: {result}");
                }

                if (s7PlcHelper.ReadVariable("VW20", out var value2))
                {
                    var result = (ushort) value2;
                    Console.WriteLine($@"Value of variable VW20 is: {result}");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            try
            {
                ushort val = 40000;
                s7PlcHelper.WriteVariable("VW20", val);
                if (s7PlcHelper.ReadVariable("VW20", out var value1))
                {
                    var result = (ushort) value1;
                    Console.WriteLine($@"Value of variable VW20 is: {result}");
                }

                if (s7PlcHelper.ReadVariable("DB1.DBW20", out var value2))
                {
                    var result = (ushort) value2;
                    Console.WriteLine($@"Value of variable DB1.DBW20 is: {result}");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            try
            {
                uint val = 10000;
                s7PlcHelper.WriteVariable("VD5", val);
                if (s7PlcHelper.ReadVariable("VD5", out var value))
                {
                    var result = (uint) value;
                    Console.WriteLine($@"Value of variable VD5 is: {result}");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}
